package Fortheenth;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;

/**
 * 彩票
 *
 * @author 陈奕君
 * @since 2025/5/13 08:24
 */

public class Test {
    private static int[] winningNumbers = Lottery();
    private static int totalPrize = 0; // 总中奖金额

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean exit = false;

        while (!exit) {

            System.out.println("XXX彩票系统");
            System.out.println("---------------------");
            System.out.println("   0. 退出系统");
            System.out.println("   1. 购买彩票");
            System.out.println("   2. 开奖");
            System.out.println("   3. 统计中奖金额");
            System.out.println("---------------------");
            System.out.print("请选择操作: ");

            int choice = scanner.nextInt();

            switch (choice) {
                case 0:
                    System.out.println("感谢使用XXX彩票系统，再见！");
                    exit = true;
                    break;
                case 1:
                    buyLottery(scanner);
                    break;
                case 2:
                    showWinningNumbers();
                    break;
                case 3:
                    showTotalPrize();
                    break;
                default:
                    System.out.println("无效选项，请重新选择！");
            }
        }

        scanner.close();
    }

    // 购买彩票功能
    public static void buyLottery(Scanner scanner) {
        System.out.println("\t  中国福利彩票");
        System.out.println("\t\t双色球");
        System.out.println("----------------------------------------");
        System.out.println("当前时间: " + SimpleDateFormat());
        System.out.print("请输入要购买的注数: ");
        int numOfTickets = scanner.nextInt();
        System.out.println("合计 " + numOfTickets * 2 + " 元");

        for (int i = 0; i < numOfTickets; i++) {
            int[] ticket = generateTicket();
            System.out.print("第" + (i + 1) + "注: ");
            printTicket(ticket);
            // 判断是否中奖并累计金额
            int prize = checkSingleWinning(ticket);
            totalPrize += prize; // 累加中奖金额
        }
    }

    // 开奖功能(使用了AI)
    public static void showWinningNumbers() {
        System.out.println("\n本期中奖号码:");
        for (int i = 0; i < 6; i++) {
            System.out.printf("%02d ", winningNumbers[i]);
            try {
                Thread.sleep(1000); // 暂停1秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.print(winningNumbers[6]);
        System.out.println();
    }

    // 生成中奖号码
    public static int[] Lottery() {
        int[] redBallNumbers = redRandom();
        int blueBallNumber = blueRandom();
        int[] winningNumbers = new int[7];
        for (int i = 0; i < 6; i++) {
            winningNumbers[i] = redBallNumbers[i];
        }
        winningNumbers[6] = blueBallNumber;
        return winningNumbers;
    }

    // 获取系统当前日期
    public static String SimpleDateFormat() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd E HH:mm:ss");
        Date now = new Date();
        return sdf.format(now);
    }

    // 生成一注彩票号码（6个红球，1个蓝球）
    public static int[] generateTicket() {
        int[] redBalls = redRandom();
        int blueBall = blueRandom();
        int[] ticket = new int[7];
        for (int i = 0; i < 6; i++) {
            ticket[i] = redBalls[i];
        }
        ticket[6] = blueBall;
        return ticket;
    }

    // 打印一注彩票号码
    public static void printTicket(int[] ticket) {
        for (int i = 0; i < 6; i++) {
            System.out.printf("%02d ", ticket[i]);
        }
        System.out.print("+ " + ticket[6]);
        System.out.println();
    }

    // 生成红球号码
    public static int[] redRandom() {
        Random random = new Random();
        int[] redBalls = new int[6];
        boolean[] used = new boolean[34];
        for (int i = 0; i < 6; i++) {
            int num;
            do {
                num = random.nextInt(33) + 1;
            } while (used[num]);
            redBalls[i] = num;
            used[num] = true;
        }

        for (int i = 0; i < 5; i++) {
            for (int j = i + 1; j < 6; j++) {
                if (redBalls[i] > redBalls[j]) {
                    int temp = redBalls[i];
                    redBalls[i] = redBalls[j];
                    redBalls[j] = temp;
                }
            }
        }
        return redBalls;
    }

    // 生成蓝球号码
    public static int blueRandom() {
        Random random = new Random();
        return random.nextInt(16) + 1;
    }

    private static int checkSingleWinning(int[] ticket) {
        int redMatchCount = 0;
        boolean blueMatch = false;

        // 判断红球
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                if (ticket[i] == winningNumbers[j]) {
                    redMatchCount++;
                    break;
                }
            }
        }
        //判断蓝球
        if (ticket[6] == winningNumbers[6]) {
            blueMatch = true;
        }

        int prize = 0;
        if (redMatchCount == 6 && blueMatch) {
            prize = 10000000;
        } else if (redMatchCount == 6) {
            prize = 5000000;
        } else if (redMatchCount == 5 && blueMatch) {
            prize = 300;
        } else if ((redMatchCount == 5 && !blueMatch) || (redMatchCount == 4 && blueMatch)) {
            prize = 200;
        } else if ((redMatchCount == 4 && !blueMatch) || (redMatchCount == 3 && blueMatch)) {
            prize = 10;
        } else if ((redMatchCount == 2 && blueMatch) || (redMatchCount == 1 && blueMatch) || (redMatchCount == 0 && blueMatch)) {
            prize = 5;
        }
        printWinningResult(redMatchCount, blueMatch, prize);
        return prize;
    }

    // 中奖结果
    private static void printWinningResult(int redMatchCount, boolean blueMatch, int prize) {
        if (redMatchCount == 6 && blueMatch) {
            System.out.println("恭喜，中一等奖！奖金: " + prize + "元");
        } else if (redMatchCount == 6) {
            System.out.println("恭喜，中二等奖！奖金: " + prize + "元");
        } else if (redMatchCount == 5 && blueMatch) {
            System.out.println("恭喜，中三等奖！奖金: " + prize + "元");
        } else if ((redMatchCount == 5 && !blueMatch) || (redMatchCount == 4 && blueMatch)) {
            System.out.println("恭喜，中四等奖！奖金: " + prize + "元");
        } else if ((redMatchCount == 4 && !blueMatch) || (redMatchCount == 3 && blueMatch)) {
            System.out.println("恭喜，中五等奖！奖金: " + prize + "元");
        } else if ((redMatchCount == 2 && blueMatch) || (redMatchCount == 1 && blueMatch) || (redMatchCount == 0 && blueMatch)) {
            System.out.println("恭喜，中六等奖！奖金: " + prize + "元");
        } else {
            System.out.println("很遗憾，未中奖");
        }
    }

    public static void showTotalPrize() {
        System.out.println("当前总中奖金额: " + totalPrize + "元");
    }
}